package com.chen.authserver.v1.controller;

import com.chen.authserver.constant.RespCode;
import com.chen.authserver.exception.ServiceException;
import com.chen.authserver.resp.SignInBody;
import com.chen.authserver.resp.RespMode;
import com.chen.authserver.resp.SignUpBody;
import com.chen.authserver.v1.DO.FinalAuthUser;
import com.chen.authserver.v1.service.TokenService;
import com.chen.authserver.v1.service.UserService;
import com.chen.authserver.v1.service.UserVerifyService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/v1/sign")
public class SignController {

    @Autowired
    UserVerifyService userVerifyService;

    @Autowired
    TokenService tokenService;

    @Autowired
    UserService userService;


    @ApiOperation("登录接口")
    @RequestMapping(value = "/in", method = RequestMethod.POST)
    public RespMode<String> loginMethod(@RequestParam String logininfo) {

        RespMode.Builder<String> builder = new RespMode.Builder<>();
        try {
            SignInBody signInBody = userVerifyService.decodeLoginInfo(logininfo);
            String accessToken = userService.userSignIn(signInBody);
            builder.success(accessToken);
        } catch (ServiceException e) {
            builder.code(e.getCode())
                    .msg(e.getErrorMsg());
        }
        return builder.build();
    }


    @ApiOperation("注册接口")
    @PostMapping("/up")
    public RespMode<String> signup(@RequestParam String signUpInfo) {
        RespMode.Builder<String> builder = new RespMode.Builder<>();
        try {
            SignUpBody signUpBody = userVerifyService.decodeSignUpInfo(signUpInfo);

            if (signUpBody != null) {
                userService.userSignUp(signUpBody);
                builder.success();
            }
        } catch (ServiceException s) {
            builder.code(s.getCode())
                    .msg(s.getErrorMsg());
        }

        return builder.build();
    }

    @ApiOperation("退出接口")
    @PostMapping("/out")
    public RespMode<String> signout(@RequestParam String account, @RequestHeader(value = "access-token") String accessToken) {

        RespMode.Builder<String> builder = new RespMode.Builder<>();

        if (tokenService.tokenValidation(accessToken, account)) {

        }

        return builder.build();
    }
}
